package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wanghaotian
 * time:2022/12/21,0021 9:44
 * description:
 */

@Service
public class DtsBrandServiceImpl implements DtsBrandService {

    @Autowired
    private DtsBrandMapper brandMapper;

    @Override
    public List<Integer> findBrandIds(Integer id) {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getAdminId,id);
        wrapper.eq(DtsBrand::getDeleted,false);
        List<DtsBrand> dtsBrands = brandMapper.selectList(wrapper);
        List<Integer> collect = dtsBrands.stream().map((brand) -> {
            return brand.getId();
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Map<String, Object>> findBrandList(List<Integer> brandIds) {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        if (brandIds != null && brandIds.size() > 0) {
            wrapper.in(DtsBrand::getId,brandIds);
        }
        List<DtsBrand> dtsBrands = brandMapper.selectList(wrapper);
        List<Map<String, Object>> collect = dtsBrands.stream().map((brand) -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", brand.getId());
            map.put("label", brand.getName());
            return map;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public IPage<DtsBrand> findPage(Integer page, Integer limit, String id, String name, String sort, String order) {
        IPage<DtsBrand> iPage = new Page<>(page,limit);
        QueryWrapper<DtsBrand> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(id)) {
            wrapper.like("id", id);
        }
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        wrapper.eq("deleted", false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("desc")) {
                wrapper.orderByDesc(sort);
            }else {
                wrapper.orderByAsc(sort);
            }
        }
        iPage = brandMapper.selectPage(iPage, wrapper);
        return iPage;
    }
}
